package hu.szyrt.bug.login;

import hu.szyrt.bug.LoginActivity;
import hu.szyrt.bug.R;
import hu.szyrt.bug.dialogs.BUGDialog;
import hu.szyrt.bug.utils.Settings;
import android.content.Context;
import android.content.Intent;
import android.view.View;

public class LoginHandler {

	private Context context;
	private Settings settings;
	
	public LoginHandler(Context context) {
		this.context = context;
		this.settings = new Settings(context);
	}
	
	
	// Functions -----------------------------
	public void showLoginDialog(String message){
		final BUGDialog dialog = new BUGDialog(context);		
		dialog.setTitle(R.string.login);
		dialog.setMessage(message);
		dialog.setNegativeButton(R.string.cancel, null);
		dialog.setPositiveButton(R.string.login, new View.OnClickListener() {				
			public void onClick(View v) {
				Intent i = new Intent(v.getContext(), LoginActivity.class);
					   i.putExtra("start_mode", LoginActivity.START_BACK);
				v.getContext().startActivity(i);
				dialog.dismiss();
			}
		});
		dialog.show();
	}
	
	// Getters & setters ---------------------
	public boolean isLoggedIn(){
		return (getLoginuser() > 0);
	}
	
	public void setLoginUser(int memberID){
		settings.save(Settings.KEY_USER_ID, memberID);
	}
	
	public int getLoginuser(){
		String userID = (String)settings.load(Settings.KEY_USER_ID);
		try{
			if(userID == null) return -1;
			else return Integer.parseInt(userID);
		}catch (Exception e) {
			return -1;
		}
	}
	
}
